package main

import (
	service "a-user-demo/user_service"
	"context"
	"fmt"
	"google.golang.org/grpc"
)

// GetUser 2 新建客户端请求方法
func GetUser(client service.UsersClient, req *service.UserGetRequest) (*service.UserGetReply, error) {
	return client.GetUser(context.Background(), req)
}

func main() {
	// 1 设定监听端口
	//client, err := grpc.DialContext(context.Background(), "127.0.0.1:3005", grpc.WithInsecure(), grpc.WithBlock())
	// 也可以使用 Dial 写法,少两个参数,结果是一样的
	client, err := grpc.Dial(":3005", grpc.WithInsecure())
	if err != nil {
		fmt.Println(err)
		return
	}

	defer func(client *grpc.ClientConn) {
		err := client.Close()
		if err != nil {
			fmt.Println(err)
		}
	}(client)
	// 3 启动客户端建立连接
	c := service.NewUsersClient(client)

	// 4 实现具体方法
	result, err := GetUser(c, &service.UserGetRequest{
		Email: "fadfa",
		Id:    10,
	})
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(result)
}
